﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GaSaiGon.GUI
{
    public partial class frmThem_CapNhatChuyenTau : Form
    {
        private string _Status;

        public string Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        private int _IDChuyenTau;

        public int IDChuyenTau
        {
            get { return _IDChuyenTau; }
            set { _IDChuyenTau = value; }
        }

        public frmThem_CapNhatChuyenTau()
        {
            InitializeComponent();
        }

        private void frmThem_CapNhatChuyenTau_Load(object sender, EventArgs e)
        {
            if (_Status == "Them")
            {
                lb_IDChuyenTau.Enabled = false;
                lb_IDChuyenTau.Visible = false;
                lbIDChuyenTau.Enabled = false;
                lbIDChuyenTau.Visible = false;
                btnThem_CapNhat.Text = "Thêm CT";
            }
            else if (_Status == "CapNhat")
            {
                lbIDChuyenTau.Text = _IDChuyenTau.ToString();
                btnThem_CapNhat.Text = "Cập nhật CT";
            }
            ChuyenTauBUS _ChuyenTauBUS = new ChuyenTauBUS();
            cbbLoaiChuyen.DataSource = _ChuyenTauBUS.DanhSachLoaiChuyen();
            cbbLoaiChuyen.DisplayMember = "TenLoaiChuyen";
            cbbLoaiChuyen.ValueMember = "MaLoaiChuyen";

            cbbThongTinGa.DataSource = _ChuyenTauBUS.ThongTinGa();
            cbbThongTinGa.DisplayMember = "IDThongTin";
            cbbThongTinGa.ValueMember = "IDThongTin";

            cbbMaTau.DataSource = _ChuyenTauBUS.TauXeLua();
            cbbMaTau.DisplayMember = "MaTau";
            cbbMaTau.ValueMember = "MaTau";

            ChuyenTauDTO _ChuyenTauDto = new ChuyenTauDTO();
            _ChuyenTauDto.ThongTinGa = int.Parse(cbbThongTinGa.SelectedValue.ToString());
            lbDiaDiemDi.Text = _ChuyenTauBUS.DiaDiemDi(_ChuyenTauDto).Rows[0]["TenDiaDiem"].ToString();
            lbDiaDiemDen.Text = _ChuyenTauBUS.DiaDiemDen(_ChuyenTauDto).Rows[0]["TenDiaDiem"].ToString();
        }

        private void btnThem_CapNhat_Click(object sender, EventArgs e)
        {
            if (_Status == "Them")
            {
                if (txtMaChuyenTau.Text == "" || cbbLoaiChuyen.Text == "" || cbbMaTau.Text == "" || cbbThongTinGa.Text == "")
                {
                    MessageBox.Show("Bạn phải nhập đầy đủ thông tin!");
                    return;
                }
                try
                {
                    ChuyenTauDTO _ChuyenTauDto = new ChuyenTauDTO();

                    _ChuyenTauDto.MaChuyenTau = txtMaChuyenTau.Text;
                    _ChuyenTauDto.LoaiChuyen = int.Parse(cbbLoaiChuyen.SelectedValue.ToString());
                    _ChuyenTauDto.NgayKhoiHanh = dtpNgayKhoiHanh.Value;
                    _ChuyenTauDto.MaTau = cbbMaTau.SelectedValue.ToString();
                    _ChuyenTauDto.ThongTinGa = int.Parse(cbbThongTinGa.SelectedValue.ToString());

                    ChuyenTauBUS _ChuyenTauBus = new ChuyenTauBUS();
                    _ChuyenTauBus.ThemChuyenTau(_ChuyenTauDto);
                    MessageBox.Show("thêm thành công!!!");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (_Status == "CapNhat")
            {
                if (txtMaChuyenTau.Text == "" || cbbLoaiChuyen.Text == "" || cbbMaTau.Text == "" || cbbThongTinGa.Text == "")
                {
                    MessageBox.Show("Bạn phải nhập đầy đủ thông tin!");
                    return;
                }
                try
                {
                    ChuyenTauDTO _ChuyenTauDto = new ChuyenTauDTO();

                    lbIDChuyenTau.Visible = true;
                    lb_IDChuyenTau.Visible = true;
                    lbIDChuyenTau.Text = _IDChuyenTau.ToString();

                    _ChuyenTauDto.IDChuyenTau = _IDChuyenTau;
                    _ChuyenTauDto.MaChuyenTau = txtMaChuyenTau.Text;
                    _ChuyenTauDto.LoaiChuyen = int.Parse(cbbLoaiChuyen.SelectedValue.ToString());
                    _ChuyenTauDto.NgayKhoiHanh = dtpNgayKhoiHanh.Value;
                    _ChuyenTauDto.MaTau = cbbMaTau.SelectedValue.ToString();
                    _ChuyenTauDto.ThongTinGa = int.Parse(cbbThongTinGa.SelectedValue.ToString());

                    ChuyenTauBUS _ChuyenTauBus = new ChuyenTauBUS();
                    _ChuyenTauBus.CapNhatChuyenTau(_ChuyenTauDto);
                    MessageBox.Show("Cập nhật thành công!!!");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void cbbThongTinGa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbThongTinGa.SelectedIndex != 0)
            {
                ChuyenTauBUS _ChuyenTauBUS = new ChuyenTauBUS();
                ChuyenTauDTO _ChuyenTauDto = new ChuyenTauDTO();
                _ChuyenTauDto.ThongTinGa = int.Parse(cbbThongTinGa.SelectedValue.ToString());
                lbDiaDiemDi.Text = _ChuyenTauBUS.DiaDiemDi(_ChuyenTauDto).Rows[0]["TenDiaDiem"].ToString();
                lbDiaDiemDen.Text = _ChuyenTauBUS.DiaDiemDen(_ChuyenTauDto).Rows[0]["TenDiaDiem"].ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
